package com.dayaokeji.rhythmschoolstudent.wiget

import android.app.Dialog
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.support.v4.app.DialogFragment
import android.support.v7.app.AppCompatDialogFragment
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.dayaokeji.rhythmschoolstudent.R
import com.dayaokeji.rhythmschoolstudent.client.common.members.SelectMembersActivity
import com.dayaokeji.rhythmschoolstudent.event.SelectMembersEvent
import com.dayaokeji.rhythmschoolstudent.wiget.adapter.AddMembersAdapter
import com.dayaokeji.server_api.domain.Member
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


/**
 *********************************************************************
 **  Created by Zeno on 2018/7/19
 *********************************************************************
 **  version 1.0
 *********************************************************************
 **  Copyright (C) dayaokeji.com 2018 -- 2020 All rights reserved.
 *********************************************************************
 ** 创建群组
 *********************************************************************
 */
class CreateGroupDialog : AppCompatDialogFragment(), View.OnClickListener {

    companion object {
        private const val CHOOSE_MEMBER_REQUEST_CODE: Int = 10001
        private const val EXTRA_DETAIL_ID = "detail_id"
        private const val EXTRA_TYPE = "type"

        fun newInstance(courseDetailId: Int,type: Int): CreateGroupDialog{
            val bundle = Bundle()
            val createGroupDialog = CreateGroupDialog()
            bundle.putInt(EXTRA_DETAIL_ID,courseDetailId)
            bundle.putInt(EXTRA_TYPE,type)
            createGroupDialog.arguments = bundle
            return createGroupDialog
        }
    }

    private lateinit var etGroupName:EditText
    private lateinit var etGroupIntro: EditText
    private lateinit var rvMembers: RecyclerView
    private lateinit var tvMemberCount: TextView

    private val participantsArrayList: ArrayList<Member> by lazy { ArrayList<Member>() }
    private val addMemberAdapter: AddMembersAdapter by lazy { AddMembersAdapter() }

    private var submitListener: SubmitListener? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        setStyle(DialogFragment.STYLE_NO_FRAME,0)
        val rootView = inflater.inflate(R.layout.dialog_create_group, container, false)
        rootView.minimumWidth = activity?.resources?.displayMetrics?.widthPixels!!

        rvMembers = rootView.findViewById(R.id.rv_members)
        etGroupName = rootView.findViewById(R.id.et_group_name)
        etGroupIntro = rootView.findViewById(R.id.et_group_intro)
        tvMemberCount = rootView.findViewById(R.id.tv_member_count)

        val btnCreateGroup = rootView.findViewById<Button>(R.id.btn_create_group)
        btnCreateGroup.setOnClickListener(this)
        return rootView
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        EventBus.getDefault().register(this)
        val courseDetailId = arguments?.getInt(EXTRA_DETAIL_ID)
        val type = arguments?.getInt(EXTRA_TYPE)
        val member = Member()
        member.localAvatar = R.mipmap.ic_add_member
        participantsArrayList.add(member)
        addMemberAdapter.setNewData(participantsArrayList)
        rvMembers.layoutManager = GridLayoutManager(activity,8)
        rvMembers.adapter = addMemberAdapter
        addMemberAdapter.onItemClickListener = BaseQuickAdapter.OnItemClickListener{ baseQuickAdapter: BaseQuickAdapter<Any, BaseViewHolder>, view: View, i: Int ->
            val info = baseQuickAdapter.data[i] as Member
            if (info.localAvatar != -1) {
                SelectMembersActivity.navigation(activity!!,courseDetailId!!,participantsArrayList,CHOOSE_MEMBER_REQUEST_CODE,type!!)
            }
        }
    }



    override fun onClick(v: View?) {
        v?.let {
            when (it.id) {
                R.id.btn_create_group -> {
                    val groupName = etGroupName.text.toString()
                    val groupIntro = etGroupIntro.text.toString()
                    val groupInfo = GroupInfo(groupName, groupIntro,addMemberAdapter.data)
                    submitListener?.click(groupInfo)
                    if (dialog.isShowing) {
                        dialog.dismiss()
                    }
                }
            }
        }

    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)
        val attributes = dialog.window.attributes
        attributes.gravity = Gravity.BOTTOM
        attributes.windowAnimations = R.style.BottomDialogAnimation
        dialog.setCanceledOnTouchOutside(true)
        dialog.onWindowAttributesChanged(attributes)
        return dialog
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    fun select(membersEvent: SelectMembersEvent) {
        if (membersEvent.members.isNotEmpty()) {
            participantsArrayList.clear()
            participantsArrayList.addAll(membersEvent.members)
            addMemberAdapter.notifyDataSetChanged()
            tvMemberCount.text = "添加成员 (${participantsArrayList.size-1})"
        }
    }

    fun setSubmitListener(submitListener: SubmitListener){
        this.submitListener = submitListener
    }

    interface SubmitListener{
        fun click(groupInfo: GroupInfo)
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
}

data class GroupInfo(val name: String,val intro: String,val member: List<Member>)